package com.leafant.samesame;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainSurfaceView extends SurfaceView  implements SurfaceHolder.Callback, Runnable{

	private boolean m_is_running;
	private SurfaceHolder m_surfaceholder;
	private static int s_screen_width, s_screen_height;
	private Paint m_paint;
	private Thread m_mainthread;
	private int m_board_row, m_board_col, m_cell_width, m_control_cell_num;
	private int m_color1, m_color2, m_color3, m_color4, m_color5, m_color6, m_color_count;
	private int[] m_color_array;
	private int[][] m_cell_color_array;
	private boolean[][] m_actived_cell;
	private Random m_random;
	private boolean m_is_selected_color;
	private int m_selected_color;
	
	public MainSurfaceView(Context context) {
		super(context);
		m_surfaceholder = getHolder();
		m_surfaceholder.addCallback(this);
		setFocusable(true);
		s_screen_width = context.getResources().getDisplayMetrics().widthPixels;
		s_screen_height = context.getResources().getDisplayMetrics().heightPixels;
	}

	private void Initialize(){
		
		m_color_count = 6;
		m_color1 = Color.argb(255, 246, 1, 0);
		m_color2 = Color.argb(255, 0, 128, 1);
		m_color3 = Color.argb(255, 246, 254, 0);
		m_color4 = Color.argb(255, 246, 140, 1);
		m_color5 = Color.argb(255, 127, 1, 127);
		m_color6 = Color.argb(255, 0, 1, 247);
		m_board_col = 10;
		m_color_array = new int[m_color_count];
		m_color_array[0] = m_color1;
		m_color_array[1] = m_color2;
		m_color_array[2] = m_color3;
		m_color_array[3] = m_color4;
		m_color_array[4] = m_color5;
		m_color_array[5] = m_color6;
		//more ...
		m_is_selected_color = false;
		m_selected_color = -1;
		m_random = new Random();
		m_cell_width = s_screen_width/m_board_col;
		m_control_cell_num = 1;
		m_board_row = s_screen_height/m_cell_width - m_control_cell_num;
		m_cell_color_array = new int[m_board_row][m_board_col];
		m_actived_cell = new boolean[m_board_row][m_board_col];		
		ResetBoard();
		m_paint = new Paint();
		m_mainthread = new Thread(this);
		m_mainthread.start();		
		m_is_running = true;
		
	}
	
	private void ResetBoard(){				
		for(int i = 0; i < m_board_row; i ++){
			for(int j = 0; j < m_board_col; j ++){
				m_cell_color_array[i][j] = m_color_array[m_random.nextInt(m_color_count)];
				m_actived_cell[i][j] = false;
			}
		}
		m_actived_cell[m_board_row-1][0] = true;
	}
	
	private void GameUpdate(){
		if(m_is_selected_color){
			m_is_selected_color = false;			
			for(int i = 0; i < m_board_row; i ++){
				for(int j = 0; j < m_board_col; j ++){
					if(CheckCellActive(i, j)){
						m_cell_color_array[i][j] = m_selected_color;
						if(CheckCellAvailable(i-1, j) && !CheckCellActive(i-1, j) && (GetCellColor(i, j) == GetCellColor(i-1, j))){
							m_actived_cell[i-1][j] = true;
						}
						if(CheckCellAvailable(i+1, j) && !CheckCellActive(i+1, j) && (GetCellColor(i, j) == GetCellColor(i+1, j))){
							m_actived_cell[i+1][j] = true;
						}
						if(CheckCellAvailable(i, j-1) && !CheckCellActive(i, j-1) && (GetCellColor(i, j) == GetCellColor(i, j-1))){
							m_actived_cell[i][j-1] = true;
						}
						if(CheckCellAvailable(i, j+1) && !CheckCellActive(i, j+1) && (GetCellColor(i, j) == GetCellColor(i, j+1))){
							m_actived_cell[i][j+1] = true;
						}
					}					
				}
			}
		}
	}
	
	private int GetCellColor(int x, int y){					
		return m_cell_color_array[x][y];
	}
	
	private boolean CheckCellAvailable(int x, int y){
		if(x <= 0 || y <= 0 || x >= m_board_row || y >= m_board_col){
			return false;
		}else
			return true;
	}
	
	private boolean CheckCellActive(int x, int y){
		return m_actived_cell[x][y];
	}
	
	public void GamePause(){
		
	}
	
	public void GameResume(){
		
	}
	
	private void GameDraw(Canvas canvas){
		DrawBoard(canvas);
		DrawControl(canvas);		
		DrawInfo(canvas);
		DrawDebug(canvas);
	}
	
	private void DrawBoard(Canvas canvas){
		for(int i = 0; i < m_board_row; i ++){
			for(int j = 0; j < m_board_col; j ++){
				m_paint.setColor(m_cell_color_array[i][j]);								
				canvas.drawRect(j*m_cell_width, i*m_cell_width, j*m_cell_width + m_cell_width, i*m_cell_width + m_cell_width, m_paint);
			}
		}		
	}
	
	private void DrawControl(Canvas canvas){
		for(int i = 0; i < m_color_count; i ++){
			m_paint.setColor(m_color_array[i]);
			canvas.drawRect(i*m_cell_width, s_screen_height - m_cell_width, i*m_cell_width+ m_cell_width, s_screen_height, m_paint);
		}
	}
	
	private void DrawInfo(Canvas canvas){
		
	}
	
	private void DrawDebug(Canvas canvas){
		m_paint.setColor(Color.BLACK);
		int debug_string_pos_x = 5;
		int debug_string_pos_y = 15;
		canvas.drawText("m_board_row == " + m_board_row, debug_string_pos_x, debug_string_pos_y += 10, m_paint);
//		canvas.drawText("m_cell_width == " + m_cell_width, debug_string_pos_x, debug_string_pos_y += 10, m_paint);
//		canvas.drawText("m_cell_width == " + m_cell_width, debug_string_pos_x, debug_string_pos_y += 10, m_paint);
	}
	
	public boolean onTouchEvent(MotionEvent e){
		if(e.getX() < m_cell_width*m_color_count){
			m_is_selected_color = true;
			m_selected_color = m_color_array[(int)e.getX()/m_cell_width];			
		}
		return true;
	}
	

	@Override
	public void run() {
		while(m_is_running){
			try{
				// Thread.sleep(20);
			}catch(Exception e){}
			GameUpdate();
			Canvas m_lockcanvas = m_surfaceholder.lockCanvas(null);
			synchronized (m_surfaceholder) {
				if(m_lockcanvas != null){
					GameDraw(m_lockcanvas);
					m_surfaceholder.unlockCanvasAndPost(m_lockcanvas);
				}
			}
		}
		
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		Initialize();
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		boolean retry = true;
		m_is_running = false;
        do {
            try {
                m_mainthread.join();
                retry = false;
            } catch (InterruptedException e) {
				e.printStackTrace();
                // we will try it again and again...
            }
        }while(retry);
		
	}
}
